package com.css.dzj.dzjfxyj.modules.distribute.Controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjfxyj.common.util.BeanUtil;
import com.css.dzj.dzjfxyj.common.util.HttpClientUtil;
import com.css.dzj.dzjfxyj.common.util.HttpContextUtils;
import com.css.dzj.dzjfxyj.common.util.R;
import com.css.dzj.dzjfxyj.modules.acquier.entity.JgFxyjWarningSignalEntity;
import com.css.dzj.dzjfxyj.modules.acquier.service.AcquierService;
import com.css.dzj.dzjfxyj.modules.distribute.entity.JgFxyjFenfaEntity;
import com.css.dzj.dzjfxyj.modules.distribute.service.DistributeService;
import com.css.dzj.dzjfxyj.modules.distribute.vo.DistributeEditVo;
import com.css.dzj.dzjfxyj.modules.distribute.vo.DistributeVo;
import com.querydsl.core.QueryResults;
import dm.jdbc.stat.support.json.JSONString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/fenfa")
@PropertySource(value = "classpath:base.properties")
public class DistributeController {
    @Autowired
    DistributeService distributeService;
    @Autowired
    BaseService baseService;
    /**
     *分页查询所有(分发)
     */
    @GetMapping(value = "/getAllDistribute")
    public R getAllDistribute(@RequestParam(required = false) Integer pageNo,
                           @RequestParam(required = false) Integer pageSize,
                           @RequestParam(name = "fxyjDomainId", required = false) String fxyjDomainId,
                           @RequestParam(name = "fxyjTitle", required = false) String fxyjTitle,
                           @RequestParam(name = "fxyjLevel", required = false) String fxyjLevel,
                           @RequestParam(name = "fxyjObjectName", required = false) String fxyjObjectName,
                           @RequestParam(name = "startTime1", required = false) String startTime1,
                           @RequestParam(name = "endTime1", required = false) String endTime1,
                           @RequestParam(name = "startTime", required = false) String startTime,
                           @RequestParam(name = "endTime", required = false) String endTime) {
        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        QueryResults results = distributeService.selectAll(fxyjDomainId,fxyjTitle,fxyjLevel,fxyjObjectName,startTime1,endTime1,startTime,endTime,pageRequest);
        baseService.logSuccess(3,"查询分发成功","2");
        return R.ok().put("data", results);
    }


    /***
     * 保存分发
     * @param riskwarningEdit
     * @return
     */
    @PostMapping("/fenFaSave")
    public R fenFaSave(@RequestBody DistributeEditVo riskwarningEdit, HttpServletRequest request) {
        try {
            distributeService.saveFenFa(riskwarningEdit,request);
            baseService.logSuccess(3,"分发保存成功","2");
            return R.ok();
        }catch (Exception e){
            baseService.logFailed(3,"分发保存失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @PostMapping("/findfenfa")
    public R findById(String id){
        DistributeVo distributeVo=distributeService.findById(id);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",distributeVo);
    }

    @PostMapping("/updFenfa")
    public R updFenfa(JgFxyjFenfaEntity jgFxyjFenfaEntity){
        try {
            distributeService.updFenfa(jgFxyjFenfaEntity);
            baseService.logSuccess(3,"更新成功","2");
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            baseService.logFailed(3,"更新失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 点击分发按钮回显数据
     * @param id
     * @return
     */
    @PostMapping("/getAcquireById")
    public R getAcquireById(String id, HttpServletRequest request){
        DistributeEditVo d = distributeService.getAcquireById(id,request);
        baseService.logSuccess(3,"分发回显数据","2");
        return R.ok().put("data",d);
    }

}
